package com.example.dmweb.chat.chatService;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelReader {

    public static void main11(String[] args) {
        // PDF文件的路径
        String filePath = "C:\\Users\\29634\\Desktop\\长乐产投人才科技202309电费明细表.pdf";

        try {
            // 加载PDF文档
            PDDocument document = PDDocument.load(new File(filePath));

            // 创建PDFTextStripper对象
            PDFTextStripper pdfStripper = new PDFTextStripper();

            // 设置按行读取
            pdfStripper.setSortByPosition(true);

            // 提取文本内容
            String text = pdfStripper.getText(document);

            // 预处理文本，去除不必要的换行符
            text = text.replaceAll("-\\r?\\n", "");  // 去除因单词被拆分产生的换行
            text = text.replaceAll("\\r?\\n", " ");  // 将其他换行替换为空格

            // 按行打印输出
            String[] lines = text.split("\\r?\\n");
            for (String line : lines) {
                System.out.println(line);
            }

            // 关闭文档
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main2(String[] args) {
        try {
            File pdfFile = new File("C:\\Users\\29634\\Desktop\\长乐产投人才科技202309电费明细表.pdf");
            PDDocument document = PDDocument.load(pdfFile);

            if (!document.isEncrypted()) {
                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition(true);
                PDFTextStripper textStripper = new PDFTextStripper();

                int numberOfPages = document.getNumberOfPages();
                List<String> pagesContent = new ArrayList<>();

                for (int i = 1; i <= numberOfPages; i++) {
                    textStripper.setStartPage(i);
                    textStripper.setEndPage(i);
                    String pageText = textStripper.getText(document);
                    pagesContent.add(pageText);
                }

                document.close();

                for (int i = 0; i < pagesContent.size(); i++) {
                    System.out.println("Page " + (i + 1) + ":\n" + pagesContent.get(i));
                }
            } else {
                System.out.println("The document is encrypted and cannot be read.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void mainexcel(String[] args) {
        String excelFilePath = "C:\\Users\\29634\\Desktop\\huaweiceshi\\general-table (8).xlsx";

        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            StringBuilder sb = new StringBuilder();

            for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row == null) continue;

                for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++) {
                    Cell cell = row.getCell(colIndex);

                    String cellValue = getCellValue(cell);
                    cellValue = cellValue.replaceAll("\\r|\\n", "");

                    if (cell != null) {
                        sb.append(cellValue);
                    } else {
                        sb.append("");
                    }

                    // 如果当前单元格是合并单元格的开始
                    if (isMergedRegion(sheet, rowIndex, colIndex)) {
                        int lastColumn = getMergedRegionLastColumn(sheet, rowIndex, colIndex);
                        colIndex = lastColumn; // 跳过合并区域
                    }

                    if (colIndex < row.getLastCellNum() - 1) {
                        sb.append("|");
                    }
                }

                sb.append("\n");
            }

            System.out.println(sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

    private static boolean isMergedRegion(Sheet sheet, int rowIndex, int colIndex) {
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
            if (region.isInRange(rowIndex, colIndex)) {
                return true;
            }
        }
        return false;
    }

    private static int getMergedRegionLastColumn(Sheet sheet, int rowIndex, int colIndex) {
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
            if (region.isInRange(rowIndex, colIndex)) {
                return region.getLastColumn();
            }
        }
        return colIndex;
    }
}
